package com.cloud.animationtestdemo.strage

import android.animation.PropertyValuesHolder
import android.animation.ValueAnimator
import android.view.View

/**
 * @author: njb
 * @date:   2025/1/1 23:33
 * @desc:   描述
 */
class TransitionAnimationStrategy :BaseAnimationStrategy{
    override fun getAlphaForProgress(progress: Float): Float {
        // 可以自定义转场过程中的透明度变化逻辑，比如淡入淡出
        return if (progress < 0.5f) progress * 2 else (1 - progress) * 2
    }

    fun animateTransition(view: View, isEnter: Boolean, duration: Long) {
        val startAlpha = if (isEnter) 0f else 1f
        val endAlpha = if (isEnter) 1f else 0f

        val alphaProperty = PropertyValuesHolder.ofFloat("alpha", startAlpha, endAlpha)

        val animator = ValueAnimator.ofPropertyValuesHolder(alphaProperty)
        animator.duration = duration
        animator.addUpdateListener { animation ->
            val currentAlpha = (animation as? ValueAnimator)?.getAnimatedValue(alphaProperty.propertyName) as? Float?: 0f
            view.alpha = currentAlpha
            val progress = animation.animatedFraction
            val alpha = getAlphaForProgress(progress)
            view.alpha = alpha
        }
        animator.start()
    }
}